package com.dhecp.project.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.system.domain.SysAccountBandDept;
import com.dhecp.project.system.mapper.SysAccountBandDeptMapper;
import com.dhecp.project.system.service.SysAccountBandDeptService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 部门绑定账户 服务层
 * @author DH-Xxj
 * @version 2022年12月12日
 */
@Service
public class SysAccountBandDeptServiceImpl implements SysAccountBandDeptService {

	@Autowired
	private SysAccountBandDeptMapper accountBandDeptMapper;

	@Autowired
	private SysDictionaryForFieldService dictionaryForFieldService;

	/**
	 * 查询
	 * @param params
	 * @return List - SysAccountBandDept
	 * @throws Exception
	 * @author DH-Xxj
	 */
	@Override
	public List<SysAccountBandDept> queryInfo(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));		//常规查询条件SQL语句        
		if (params.get("field") == null || params.get("field") == ""){
			String orderBy = "accountId asc";
			PageUtils.startPageOrder(orderBy);
		}else{
			PageUtils.startPage();	
		}
		return accountBandDeptMapper.findInfo(params);
	}
	
	/**
	 * 通过guid查询单条记录
	 * @param guid
	 * @return SysDepotInfo
	 * @author lwl
	 */
	@Override
	public SysAccountBandDept queryByGuid(String guid) {
		return accountBandDeptMapper.findByGuid(guid);
	}

    /**
     * 新增
     * @param infoItems 必要参数：对应实体类的json格式字符串
     * @return
     * @author DH-Xxj
     */
	@Override
	public AjaxResult insertInfo(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<SysAccountBandDept> accountBandDeptList = JSON.parseArray(params.get("infoItems").toString(), SysAccountBandDept.class);
		
		if (accountBandDeptList == null || accountBandDeptList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (SysAccountBandDept accountBandDept : accountBandDeptList) {
			accountBandDept.setCreateUser(ShiroUtils.getUserId());
			accountBandDept.setCreateTime(new Date());
			
			accountBandDeptMapper.insertInfo(accountBandDept);
		}
		return AjaxResult.success("操作成功");
	}

	/**
	 * 删除
     * @param infoItems 必要参数：对应实体类的json格式字符串
	 * @return
	 * @author DH-Xxj
	 */
	@Override
	public AjaxResult deleteInfo(Map<String, Object> params) {
		List<SysAccountBandDept> accountBandDeptList = JSON.parseArray(params.get("infoItems").toString(), SysAccountBandDept.class);
		if (accountBandDeptList == null || accountBandDeptList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (SysAccountBandDept accountBandDept : accountBandDeptList) {
			accountBandDeptMapper.deleteInfo(accountBandDept);
		}
		return AjaxResult.success("操作成功");
	}

	/**
	 * 通过guid修改信息
     * @param infoItems 必要参数：对应实体类的json格式字符串
	 * @return
	 * @author DH-Xxj
	 */
	@Override
	public AjaxResult updateByGuid(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<SysAccountBandDept> accountBandDeptList = JSON.parseArray(params.get("infoItems").toString(), SysAccountBandDept.class);
		if (accountBandDeptList == null || accountBandDeptList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (SysAccountBandDept accountBandDept : accountBandDeptList) {
			accountBandDept.setUpdateUser(ShiroUtils.getUserId());
			accountBandDept.setUpdateTime(new Date());
			accountBandDeptMapper.updateByGuid(accountBandDept);
		}
		return AjaxResult.success("操作成功");
	}
	
}
